<template>
  <Modal v-model="show" class="modal-login" title="用户登录" width=320 :closable="false" :mask-closable="false">
        <Input name="user" :value="userAccount()" :disabled="true" placeholder="用户名" type="text"></Input>
        <Input name="password" v-model="password" placeholder="密码" type="password"></Input>
        <div slot="footer" class="button-panel-button">
            <i-button @click="login" :disabled="!loginCheck" type="primary">确认</i-button>
        </div>
  </Modal>
</template>
<script>
import $ from "@/common/index";
import md5 from "@/login/md5";
//超时重登录功能，待与登录方法整合
export default {
  props: {
    value: {}
  },
  data() {
    return {
      submitPath: "/service/{app}/signin?name={name}&password={password}",
      show: false,
      password: ""
    };
  },
  computed: {
    loginCheck() {
      if (this.password !== null && this.password.length) {
        return true;
      }
    }
  },
  watch: {
    value(v) {
      this.password = null;
      this.show = v;
    },
    show(v) {
      if (v) {
        this.$Message.warning("登录超时需要重新输入密码");
      }
      this.$emit("input", v);
    }
  },
  methods: {
    login() {
      $.link(
        this.submitPath,
        {},
        {
          param: {
            name: this.userAccount(),
            password: md5(this.userAccount(), this.password)
          }
        }
      ).then(res => {
        this.$Message.success("身份验证成功");
        //重试回调
        this.close();
        if (this.$linkSetting && this.$linkSetting.retry) {
          this.$nextTick(() => {
            this.$linkSetting.retry();
          });
        }
      });
    },
    close() {
      this.show = false;
    },
    userAccount() {
      return $.User.account_code;
    }
  },
  created() {
    //设置全局方法
    $.loginCheck = (res, setting) => {
      this.show = true;
      this.$linkSetting = setting;
    };
  }
};
</script>

<style lang="less">
.modal-login {
  > .ivu-modal-mask {
    z-index: 10000;
    background-color: rgba(55, 55, 55, 0.8);
  }
  > .ivu-modal-wrap {
    z-index: 10000;
  }
  .ivu-input-wrapper {
    margin-bottom: 18px;
  }
  .ivu-modal-header {
    text-align: center;
  }
  .button-panel-button {
    text-align: center;
  }
  .message {
    margin-top: -16px;
  }
}
</style>
